﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports System.IO
Imports Boogaloo.Core.UI
Imports Boogaloo.Admin.UI.Controls
Imports Boogaloo.Core
Imports Boogaloo.Core.BLL


Namespace UI.Webpages


    Partial Public Class MasterPageEditor
        Inherits BaseAdminPage

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            If Not Page.IsPostBack Then

                If Groove.admin_root.master_templates.Count = 0 Then
                    Response.Redirect(String.Format("{0}MasterPages.aspx", Groove.path_admin_host))
                    Response.End()
                End If


                ddl_site_masters.DataSource = Groove.admin_root.master_templates
                ddl_site_masters.DataValueField = "master_template_id"
                ddl_site_masters.DataTextField = "name"
                ddl_site_masters.DataBind()


                If Not String.IsNullOrEmpty(Request.QueryString("master")) Then ddl_site_masters.SelectedValue = Request.QueryString("master")

                BindPage()

            End If

        End Sub

        Private Sub BindPage()

            Dim mt As MasterTemplate = MasterTemplate.GetMasterTemplate(CType(ddl_site_masters.SelectedValue, Integer))

            If mt IsNot Nothing Then

                txt_source_editor.Text = Groove.GetTextFileContents(mt.path)

            End If

        End Sub

        Protected Sub ddl_site_masters_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddl_site_masters.SelectedIndexChanged

            BindPage()

        End Sub

        Protected Sub cwiz_save_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cwiz_save.Click

            Dim mt As MasterTemplate = MasterTemplate.GetMasterTemplate(CType(ddl_site_masters.SelectedValue, Integer))
            If mt IsNot Nothing Then

                Groove.WriteTextFileContents(mt.path, txt_source_editor.Text)

            End If

            Response.Redirect(String.Format("{0}MasterPageEditor.aspx?master={1}", Groove.path_admin_host, mt.master_template_id.ToString))

        End Sub

    End Class

End Namespace